<cfcomponent output="false">
	
	<!--- --->
	
	<cffunction name="loginTraditional" access="public" output="false" returntype="string">
		<cfargument name="email" required="true" />
		<cfargument name="password" required="true" />

		<cfset var local = {} />

		<cfset local.user = application.g.loginGateway.login(argumentCollection=arguments) />
		
		<cfif local.user.recordcount gt 0 and local.user.password eq arguments.password>
			<cfset local.userID = local.user.id />
			<cfset application.s.userService.setSessionUser(local.userID) />
			<cfset $.session.addMessage("Login Successful.") />
		<cfelse>
			<cfset local.userID = -1 />
			<cfset $.session.addMessage("Login Failed.") />
		</cfif>
		<cfset session.userID = local.userID />
		
		<cfreturn local.userID />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="logout" access="public" output="false" returntype="void">

		<cfset structClear(session) />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="facebookLoginSuccess" access="public" output="false" returntype="void">
		<cfif structKeyExists(session,"facebookAccessToken") and session.facebookAccessToken neq ''>
			<!--- fetch the user email --->
			<cftry>
				<!--- webservice call to facebook --->
				<cfset local.thisURL = "https://graph.facebook.com/me?access_token=#session.facebookAccessToken#">
				<cfhttp url="#local.thisURL#" throwonerror="true">
				<cfset local.response = cfhttp.filecontent />
				<cfset local.data = deserializeJSON(local.response) />
				
				<cfif not structKeyExists(local.data,"first_name") or not structKeyExists(local.data,"last_name") or not structKeyExists(local.data,"email")>
					<cfset $.session.addMessage("Facebook profile data incomplete.") />
					<cfthrow message="bad data from facebook">
				</cfif>

				<!--- see if that email exists as a user--->
				<cfset local.user = application.s.userService.getUserByEmail(local.data.email) />
				<!--- if not create the user and log them in --->
				<cfif local.user.recordcount gt 0>
					<!--- if so, log the user in --->
					<cfset $.session.addMessage("Logged In via Facebook.") />
					<cfset loginTraditional(email=local.user.email,password=local.user.password) />
				<cfelse>
					<!--- or create a record then log them in --->
					<cfset local.userID = _createFacebookUser(local.data) />
					<cfset application.s.userService.setSessionUser(local.userID) />

					<cfif local.userID eq '-1'>
						<cfthrow message="duplicate userID" />
					</cfif>
					<cfset loginTraditional(session.user.email,left(session.facebookAccessToken,10)) />
				</cfif>
				
				<cfcatch>
					<cfset $.session.addMessage("Error:loginService.facebookLoginSuccess #cfcatch.message#") />
					<cflocation url="#$.site.getVar("homeURL")#" addToken="no" />
				</cfcatch>
			</cftry>
		</cfif>
	</cffunction>
	
	<!--- --->
	
	<cffunction name="_createFacebookUser" access="private" output="false" returntype="string">
		<cfargument name="data" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.valid = true />
		
		<cfset local.user = {} />
		<!--- email --->
		<cfif structKeyExists(arguments.data,"email") and arguments.data.email neq "">
			<cfset local.user.email = arguments.data.email />
		<cfelse>
			<cfset local.valid = false />
		</cfif>
		
		<!--- password --->
		<cfset local.user.password = left(session.facebookAccessToken,10) />
		
		<!--- firstname --->
		<cfif structKeyExists(arguments.data,"first_name") and arguments.data.first_name neq "">
			<cfset local.user.first_name = arguments.data.first_name />
		<cfelse>
			<cfset local.user.first_name = "unknown" />
		</cfif>
		
		<!--- middlename --->
		<cfif structKeyExists(arguments.data,"middle_name") and arguments.data.middle_name neq "">
			<cfset local.user.middle_name = arguments.data.middle_name />
		</cfif>
		
		<!--- last name --->
		<cfif structKeyExists(arguments.data,"last_name") and arguments.data.last_name neq "">
			<cfset local.user.last_name = arguments.data.last_name />
		<cfelse>
			<cfset local.user.last_name = "unknown" />
		</cfif>
		
		<!--- city --->
		<cfif structKeyExists(arguments.data,"location") and structKeyExists(arguments.data.location,"name") and arguments.data.location.name neq "">
			<cfset local.user.city = listFirst(arguments.data.location.name) />
		</cfif>
		
		<!--- state --->
		<cfif structKeyExists(arguments.data,"location") and structKeyExists(arguments.data.location,"name") and arguments.data.location.name neq "">
			<cfset local.state = application.s.resourceService.getStates(name=listLast(arguments.data.location.name)) />
			<cfif local.state.recordcount gt 0 >
				<cfset local.user.state_id = local.state.id />
			</cfif>
		</cfif>
		
		<cfset local.userID = application.s.userService.createAccount(argumentCollection=local.user) />

		<cfreturn local.userID />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveBetaEmail" access="public" output="false" returntype="string">
		<cfargument name="email" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.result = false />
		
		<cfquery name="local.emails" datasource="fundrdb">
			select id from beta_emails where email = <cfqueryparam value="#arguments.email#" cfsqltype="varchar" maxLength="50">
		</cfquery>
		
		<cfif arguments.email neq '' and local.emails.recordcount eq 0>
			<cfset $.database.save(
				table="beta_emails",
				id="",
				email=arguments.email
			) />
			<cfset local.result = true />
		</cfif>
		
		<cfreturn local.result />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getBetaEmailCounts" access="public" output="false" returntype="string">
		
		<cfset var local = {} />
		
		<cfquery name="local.emails" datasource="fundrdb">
			select count(id) as counts from beta_emails
		</cfquery>
		
		<cfreturn local.emails.counts />
		
	</cffunction>
	
	<!--- --->

</cfcomponent>